package com.exartisan.network;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.exartisan.bean.FbComment;
import com.exartisan.bean.FbUser;

public class FacebookResponseParser {
	
	public static ArrayList<FbComment> getPublicComments(String jsonResponse, String url){
		ArrayList<FbComment> comments = new ArrayList<FbComment>();
		String id;
		String message;
		String created_time;
		FbUser fbUser;
		int like_count;
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonId = jsonRp.getJSONObject(url);
			//JSONObject jsonComments = jsonId.getJSONObject("comments");
			JSONArray jsonData = jsonId.getJSONArray(NetworkUtility.DATA);
			for (int i = 0; i < jsonData.length(); i++) {
				JSONObject jsonElement = jsonData.getJSONObject(i);
				id = jsonElement.getString(NetworkUtility.FACEBOOK_COMMENT_ID);
				message = jsonElement.getString(NetworkUtility.FACEBOOK_COMMENT);
				created_time = jsonElement.getString(NetworkUtility.CREATED_TIME);
				JSONObject jsonFrom = jsonElement.getJSONObject(NetworkUtility.FACEBOOK_FROM);
				fbUser = new FbUser();
				fbUser.setId(jsonFrom.getString(NetworkUtility.FACEBOOK_USER_ID));
				fbUser.setName(jsonFrom.getString(NetworkUtility.FACEBOOK_USER_NAME));
				like_count = jsonElement.getInt(NetworkUtility.FACEBOOK_LIKE_COUNT);
				FbComment fbComment = new FbComment(id, message, created_time, fbUser, like_count);
				comments.add(fbComment);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comments;
	}
	
	public static ArrayList<FbComment> getComments(String jsonResponse){
		ArrayList<FbComment> comments = new ArrayList<FbComment>();
		String id;
		String message;
		String created_time;
		FbUser fbUser;
		int like_count;
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONArray jsonData = jsonRp.getJSONArray(NetworkUtility.DATA);
			for (int i = 0; i < jsonData.length(); i++) {
				JSONObject jsonElement = jsonData.getJSONObject(i);
				id = jsonElement.getString(NetworkUtility.FACEBOOK_COMMENT_ID);
				message = jsonElement.getString(NetworkUtility.FACEBOOK_COMMENT);
				created_time = jsonElement.getString(NetworkUtility.CREATED_TIME);
				JSONObject jsonFrom = jsonElement.getJSONObject(NetworkUtility.FACEBOOK_FROM);
				fbUser = new FbUser();
				fbUser.setId(jsonFrom.getString(NetworkUtility.FACEBOOK_USER_ID));
				fbUser.setName(jsonFrom.getString(NetworkUtility.FACEBOOK_USER_NAME));
				like_count = jsonElement.getInt(NetworkUtility.FACEBOOK_LIKE_COUNT);
				FbComment fbComment = new FbComment(id, message, created_time, fbUser, like_count);
				comments.add(fbComment);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return comments;
	}
	
	public static String getCommentPageURL(String jsonResponse, boolean nextCommentPage){
		String commentPageURL = "";
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonPaging = jsonRp.getJSONObject(NetworkUtility.PAGING);
			if (nextCommentPage) {
				commentPageURL = jsonPaging.getString(NetworkUtility.FACEBOOK_NEXT_CMT_PAGE);
			} else commentPageURL = jsonPaging.getString(NetworkUtility.FACEBOOK_PRE_CMT_PAGE);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return commentPageURL;
	}
	
	public static String getPictureURL(String jsonResponse){
		String pictureURL = "";
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonPicture = jsonRp.getJSONObject(NetworkUtility.FACEBOOK_PROFILE_PICTURE);
			JSONObject jsonData = jsonPicture.getJSONObject(NetworkUtility.DATA);
			pictureURL = jsonData.getString(NetworkUtility.FACEBOOK_PICTURE_URL);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pictureURL;
	}
	
	public static int getCount(String json_summary_response){
		try {
			JSONObject summaryObject = new JSONObject(json_summary_response).getJSONObject(NetworkUtility.SUMMARY);
			return summaryObject.getInt(NetworkUtility.TOTAL_COUNT);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
}
